﻿@using System.Web.UI.WebControls
@using WSS.Pub
@{
    ViewBag.Title = "试卷列表";
}
<div class="container" style="margin-top: 20px">
    <div class="panel panel-primary">
        <div class="panel-heading">试卷列表</div>
        <div class="panel-body form-inline">
            <label>分类：</label>
            <div class="btn-group">
                <button class="btn btn-default  dropdown-toggle" type="button" data-toggle="dropdown">
                    <span id="CATE_NO_List_SPAN">全部</span>  <span class="caret"></span>
                </button>
                <ul class="dropdown-menu" id="btn_testcate_list_menu">
                </ul>
                @Html.Hidden("CATE_LIST_NO")
            </div>
            <label style="margin-left: 5px; margin-right: 5px;">作废状态：</label>
            <div class="btn-group" style="margin-left: 5px; margin-right: 5px;">
                <button class="btn btn-default  dropdown-toggle" type="button" data-toggle="dropdown">
                    <span id="sch_flagInvaild_Span">全部</span>  <span class="caret"></span>
                </button>
                <ul class="dropdown-menu">
                    <li val="0"><a href="#" onclick="onClickMenu('sch_flagInvaild_Span',0,'否','sch_flagInvaild')">否</a></li>
                    <li val="1"><a href="#" onclick="onClickMenu('sch_flagInvaild_Span',1,'是','sch_flagInvaild')">是</a></li>
                    <li val=""><a href="#" onclick="onClickMenu('sch_flagInvaild_Span','','全部','sch_flagInvaild')">全部</a></li>
                </ul>
                @Html.Hidden("sch_flagInvaild")
            </div>
            <label style="margin-left: 5px; margin-right: 5px;">名称：<input id="sch_testname" type="text" style="width: 200px; height: 33px; border: 1px solid #ebebeb; border-radius: 4px;" /></label>

            @Html.MyButton("btn_sch", "搜索")

            @Html.MyButton("btn_add", "新增", "primary")

            @Html.MyButton("btn_edit", "编辑", "danger")

            @Html.MyButton("btn_del", "删除", "warning")

            @Html.MyButton("btn_imp", "导入试卷", "info")
        </div>

        <table id="maingrid"></table>
        <div class="panel-footer">
            <ul id="page1"></ul>
        </div>
    </div>
</div>

<div id="hid_add_form" class="hide">
    <form class="form-horizontal" role="form" id="form1">
        <div>
            <div class="form-group">
                <label for="" class="control-label">试卷分类：</label>
                <div class="btn-group" style="margin-left: 15px">
                    <button class="btn btn-default btn-sm  dropdown-toggle" type="button" data-toggle="dropdown">
                        <span id="CATE_NO_SPAN">请选择</span>  <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" id="btn_testcate_menu">
                    </ul>
                    @Html.Hidden("CATE_NO")
                </div>

            </div>
            <div class="form-group">
                <label for="TEST_NAME" class="control-label">试卷名称：</label>
                <div class="div_text">
                    <input type="text" class="form-control notnull" id="TEST_NAME" name="TEST_NAME">
                </div>

            </div>
            <div class="form-group">
                <label for="TEST_YEAR" class="control-label">试卷年份：</label>
                <div class="div_text">
                    <input type="text" class="form-control notnull" id="TEST_YEAR" name="TEST_YEAR" value="@ViewBag.CurYear">
                </div>

            </div>
            <div class="form-group">
                <label for="ORDER_NO" class="control-label">排序序号：</label>
                <div class="div_text">
                    <input type="text" class="form-control" id="ORDER_NO" name="ORDER_NO"
                        onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
                </div>

            </div>
            <div class="form-group">
                <label for="TEST_LEVEL" class="control-label">试卷等级：</label>
                <div class="btn-group" style="margin-left: 15px">
                    <button class="btn btn-default btn-sm  dropdown-toggle" type="button" data-toggle="dropdown">
                        <span id="TEST_LEVEL_SPAN">请选择</span>  <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" id="TEST_LEVEL_MENU">
                        <li val=""><a href="#" onclick="onClickMenu('TEST_LEVEL_SPAN','无','无','TEST_LEVEL')">无</a></li>
                        <li val="简单"><a href="#" onclick="onClickMenu('TEST_LEVEL_SPAN','简单','简单','TEST_LEVEL')">简单</a></li>
                        <li val="中等"><a href="#" onclick="onClickMenu('TEST_LEVEL_SPAN','中等','中等','TEST_LEVEL')">中等</a></li>
                        <li val="复杂"><a href="#" onclick="onClickMenu('TEST_LEVEL_SPAN','复杂','复杂','TEST_LEVEL')">复杂</a></li>
                    </ul>
                    @Html.Hidden("TEST_LEVEL")
                </div>
            </div>
            <div class="form-group">
                <label for="TEST_NAME" class="control-label">是否作废：</label>
                <div class="btn-group" style="margin-left: 15px">
                    <button class="btn btn-default btn-sm  dropdown-toggle" type="button" data-toggle="dropdown">
                        <span id="FLAG_INVALID_SPAN">请选择</span>  <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" id="FLAG_INVALID_MENU">
                        <li val="0"><a href="#" onclick="onClickMenu('FLAG_INVALID_SPAN',0,'否','FLAG_INVALID')">否</a></li>
                        <li val="1"><a href="#" onclick="onClickMenu('FLAG_INVALID_SPAN',1,'是','FLAG_INVALID')">是</a></li>
                    </ul>
                    @Html.Hidden("FLAG_INVALID")
                </div>


            </div>
            <div class="form-group">
                <label for="TEST_NAME" class="control-label">描述：</label>
                <div class="div_text">
                    <textarea class="form-control" rows="3" name="TEST_CONTENTS" id="TEST_CONTENTS"></textarea>

                </div>

            </div>
            @Html.Hidden("TEST_ID")


        </div>
    </form>
</div>
@section css
{
    <style type="text/css">
        .form-group
        {
            width: 530px;
        }

            .form-group label
            {
                width: 100px;
                float: left;
                font-weight: normal;
            }

        .div_text
        {
            float: left;
            margin-left: 15px;
            width: 400px;
        }
    </style>
}

@section script
{

    <script src="@Url.Content("~/Scripts/plugins/bootstrap-paginator.min.js")"></script>
    <script src="@Url.Content("~/Scripts/plugins/jquery.grid.js")"></script>

    <script type="text/javascript">
        $(function () {
            $("#btn_testcate_menu").load("/testCenter/GetTestCategoryToSelect", { addData: 1, valControlID: 'CATE_NO', textControlID: 'CATE_NO_SPAN' }, function () { });
            $("#btn_testcate_list_menu").load("/testCenter/GetTestCategoryToSelect", { selectall: 1, valControlID: 'CATE_LIST_NO', textControlID: 'CATE_NO_List_SPAN' },
                function () { });

            $("#btn_sch").click(function () {
                sch();
            });

            $("#btn_imp").click(function () {
                window.location.href = "/testcenter/import";
            });

            function resetForm() {
                $("#form1").get(0).reset();
            }

            $("#btn_add").click(function () {
                resetForm();
                $("#ORDER_NO").val('@ViewBag.OrderNO');
                var html = $("#hid_add_form").addClass("show").removeClass("hide");
                mainfn.dialog("添加试卷", html, save, false);
            });

            $("#btn_edit").click(function () {
                var row = $("#maingrid").bootgrid("getGridSelRow");
                if (row) {
                    bindForm(row);
                }
            });
            $("#btn_del").click(function () {
                var rows = $("#maingrid").bootgrid("getGridSelRows");
                var ids = new Array();
                $.each(rows, function (i, row) {
                    ids.push(row["TEST_ID"]);
                });
                var delConfirm = confirm("删除试卷，对应的试题也会被删除，确认删除？！");
                if (delConfirm) {
                    $.post("/testCenter/delTest", { id: ids.join(',') }, function (data) {
                        if (data.flag) {
                            sch();
                        } else {
                            mainfn.msg(data.msg);
                        }
                    });
                }
            });

            /*
                          编辑状态下，绑定select的值
                        */
            function bingSelectVal() {
                var testLevel = $("#TEST_LEVEL").val();
                if (testLevel != "" && testLevel != null) {
                    $("#TEST_LEVEL_SPAN").text(testLevel);
                } else {
                    $("#TEST_LEVEL_SPAN").text("请选择");
                }
                var flagInvalid = $("#FLAG_INVALID").val();
                if (flagInvalid != "" && flagInvalid != null) {
                    $("#FLAG_INVALID_SPAN").text(flagInvalid == 0 ? "否" : "是");
                } else {
                    $("#FLAG_INVALID_SPAN").text("请选择");
                }
                var cateNo = $("#CATE_NO").val();
                if (cateNo != "" && cateNo != null) {
                    $("#CATE_NO_SPAN").text($("#btn_testcate_menu li[CateNo='" + cateNo + "']").find("a").html());
                } else {
                    $("#CATE_NO_SPAN").text("请选择");
                }
            }

            function bindForm(row) {
                //获取当前数据详情
                $.ajax({
                    url: "/testCenter/GetOneTestModel",
                    data: { id: row["TEST_ID"] },
                    async: false,
                    type: 'POST',
                    success: function (data) {
                        row = data;
                    }
                });
                mainfn.loadForm($("#form1"), row);
                var html = $("#hid_add_form").addClass("show").removeClass("hide");
                bingSelectVal();
                mainfn.dialog("编辑试卷", html, save, false);
            }

            function save() {

                if (mainfn.checkForm($("#form1"))) {
                    return false;
                }
                if ($("#FLAG_INVALID").val() == "") {
                    $("#FLAG_INVALID").val(0);
                }
                var s = $("#form1").serialize();
                var url = "/testCenter/SaveTest";
                $.post(url, s, function (msg) {
                    mainfn.msg(msg.msg);
                    if (msg.flag) {
                        sch();
                    }
                });

                $("#form1").get(0).reset();

                return true;
            }


            function init(parameters) {

                $("#maingrid").bootgrid({
                    url: '/testCenter/GetTestList',
                    pager: "page1",
                    multiple: true,
                    headers: ["ID", "年份", "试卷分类", "试卷名称", "上传人", "试题总数", "作废状态", "操作"],
                    cols: [
                        { id: 'TEST_ID', hidden: true },
                        { id: 'TEST_YEAR' },
                        { id: 'CATE_NAME' },
                        {
                            id: 'TEST_NAME', align: 'left',
                            formatter: function (rowdata) {
                                var tesTName = rowdata["TEST_NAME"];
                                if (tesTName.length > 40) {
                                    return "<span title='" + tesTName + "'>" + tesTName.substring(0, 40) + "...</span>";
                                } else {
                                    return tesTName;
                                }


                            }
                        },
                        {
                            id: 'USER_NAME'
                        },
                        { id: 'QCOUNT' },
                        { id: 'FLAG_INVALID', formatter: { type: "select", fmt: { "0": "使用", "1": "<span style='color:red;'>作废</span>" } } },
                        {
                            id: "Opt_AddQuestion",
                            align: "center",
                            formatter: function (rowdata) {
                                return "<a href='#' onclick=\"ViewQuestion('" + rowdata["TEST_ID"] + "')\">管理</a>";

                            }
                        }
                    ],
                    ondbclick: function (e, id, row) {
                        bindForm(row);
                    }
                });

            }

            init();
        });

        function sch() {
            $("#maingrid").bootgrid("setGridParam", { sch_testname: $("#sch_testname").val(), cateno: $("#CATE_LIST_NO").val(), flagInvaild: $("#sch_flagInvaild").val() });
            $("#maingrid").bootgrid("reloadGrid", { page: 1 });
        }

        function onClickMenu(menuId, val, text, setControlId) {
            $("#" + menuId).text(text);
            $("#" + setControlId).val(val);
            if (menuId == "CATE_NO_List_SPAN" || menuId == "sch_flagInvaild_Span") {
                sch();
            }

        }

        function ViewQuestion(testId) {
            location.href = "/testCenter/TestQList?testId=" + testId;
        }
    </script>
}